package com.alatus;

public class Vector3D {
//    矢量在XYZ轴上的分量
    public float x,y,z;
//    构造函数,传入矢量的三个分类
    public Vector3D(float x,float y,float z){
        this.x = x;
        this.y = y;
        this.z = z;
    }
//    把XYZ轴上的分量设置为v
    public void set(Vector3D v){
        this.x = v.x;
        this.y = v.y;
        this.z = v.z;
    }
//    矢量加
    public void add(Vector3D v){
        this.x += v.x;
        this.y += v.y;
        this.z += v.z;
    }
//    矢量减
    public void sub(Vector3D v){
        this.x -= v.x;
        this.y -= v.y;
        this.z -= v.z;
    }
//    矢量点之和,结果代表两个矢量之间的相似程度,用来判断多边形的亮度
    public float dot(Vector3D v1,Vector3D v2){
        return v1.x*v2.x+v1.y*v2.y+v1.z*v2.z;
    }
//    矢量叉积,求一个与这两个矢量都垂直的矢量,求多边形的法矢量
    public void cross(Vector3D v1,Vector3D v2){
        x = v1.y*v2.z-v1.z*v2.y;
        y = v1.z*v2.x-v1.x*v2.z;
        z = v1.x*v2.y-v1.y*v2.x;
    }
//    求矢量长度
    public float getLength(){
        return (float)Math.sqrt(x*x+y*y+z*z);
    }
//    将矢量单位化
    public void unit(){
        float l = getLength();
        x = x/l;
        y = y/l;
        z = z/l;
    }
//    将矢量乘以一个标量
    public void scale(float s){
        x *= s;
        y *= s;
        z *= s;
    }
//    绕Y轴旋转矢量,使其顺时针旋转指定角度
    public void rotate_Y(int angle){
        float sin = LookupTable.sin[angle];
        float cos = LookupTable.cos[angle];
        float old_X = x;
        float old_Z = z;
        x = old_X*cos-old_Z*sin;
        z = old_X*sin+old_Z*cos;
    }
//    绕X轴旋转矢量,使其顺时针旋转指定角度
    public void rotate_X(int angle){
        float sin = LookupTable.sin[angle];
        float cos = LookupTable.cos[angle];
        float old_Y = y;
        float old_Z = z;
        y = old_Y*cos-old_Z*sin;
        z = old_Y*sin+old_Z*cos;
    }
//    绕Z轴旋转矢量,使其顺时针旋转指定角度
    public void rotate_Z(int angle){
        float sin = LookupTable.sin[angle];
        float cos = LookupTable.cos[angle];
        float old_X = x;
        float old_Y = y;
        x = old_X*cos-old_Y*sin;
        y = old_X*sin+old_Y*cos;
    }
}
